package cn.iocoder.yudao.module.ems.controller.admin.supplier;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.SupplierListReqVO;
import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.SupplierSimpleRespVO;
import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.SupplierRespVO;
import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.ContactRespVO;
import cn.iocoder.yudao.module.ems.convert.supplier.SupplierConvert;
import cn.iocoder.yudao.module.ems.convert.supplier.ContactConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.ContactDO;
import cn.iocoder.yudao.module.ems.service.supplier.ContactService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.ems.convert.supplier.SupplierConvert;
import cn.iocoder.yudao.module.ems.service.supplier.SupplierService;

@Api(tags = "管理后台 - 供应商")
@RestController
@RequestMapping("/ems/supplier")
@Validated
public class SupplierController {

    @Resource
    private SupplierService supplierService;
    @Resource
    private ContactService contactService;

    @PostMapping("/create")
    @ApiOperation("创建供应商")
    @PreAuthorize("@ss.hasPermission('ems:supplier:create')")
    public CommonResult<Long> createSupplier(@Valid @RequestBody SupplierCreateReqVO createReqVO) {
        return success(supplierService.createSupplier(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新供应商")
    @PreAuthorize("@ss.hasPermission('ems:supplier:update')")
    public CommonResult<Boolean> updateSupplier(@Valid @RequestBody SupplierUpdateReqVO updateReqVO) {
        supplierService.updateSupplier(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除供应商")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:supplier:delete')")
    public CommonResult<Boolean> deleteSupplier(@RequestParam("id") Long id) {
        supplierService.deleteSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得供应商")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:supplier:query')")
    public CommonResult<SupplierRespVO> getSupplier(@RequestParam("id") Long id) {
        SupplierDO supplier = supplierService.getSupplier(id);
        List<ContactDO> contactList = contactService.getContactList(id);
        SupplierRespVO resp = new SupplierRespVO();
        resp.setId(supplier.getId());
        resp.setCompany(supplier.getCompany());
        resp.setAdress(supplier.getAdress());
        resp.setGrade(supplier.getGrade());
        resp.setPhone(supplier.getPhone());
        resp.setName(supplier.getName());
        resp.setWebsite(supplier.getWebsite());
        resp.setCreateTime(supplier.getCreateTime());
        resp.setRemarks(supplier.getRemarks());
        resp.setStatus(supplier.getStatus());
        resp.setContact(ContactConvert.INSTANCE.convert(contactList));
        return success(resp);
    }

    @GetMapping("/list")
    @ApiOperation("获得供应商列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('ems:supplier:query')")
    public CommonResult<List<SupplierRespVO>> getSupplierList(@RequestParam("ids") Collection<Long> ids) {
        List<SupplierDO> list = supplierService.getSupplierList(ids);
        return success(SupplierConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得供应商分页")
    @PreAuthorize("@ss.hasPermission('ems:supplier:query')")
    public CommonResult<PageResult<SupplierRespVO>> getSupplierPage(@Valid SupplierPageReqVO pageVO) {
        PageResult<SupplierDO> pageResult = supplierService.getSupplierPage(pageVO);
        return success(SupplierConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出供应商 Excel")
    @PreAuthorize("@ss.hasPermission('ems:supplier:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplierExcel(@Valid SupplierExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SupplierDO> list = supplierService.getSupplierList(exportReqVO);
        // 导出 Excel
        List<SupplierExcelVO> datas = SupplierConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "供应商.xls", "数据", SupplierExcelVO.class, datas);
    }

    @DeleteMapping("/deleteInfo")
    @ApiOperation("删除联系人信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:supplier:delete')")
    public CommonResult<Boolean> deleteContact(@RequestParam("id") Long id) {
        contactService.deleteContact(id);
        return success(true);
    }

    @PutMapping("/updateInfo")
    @ApiOperation("更新联系人信息")
    @PreAuthorize("@ss.hasPermission('ems:supplier:update')")
    public CommonResult<Boolean> updateContact(@Valid @RequestBody ContactRespVO updateReqVO) {
        contactService.updateContact(updateReqVO);
        return success(true);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取设备类别精简信息列表", notes = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<SupplierSimpleRespVO>> getSimpleSuppliers() {
        // 获得分类列表，只要开启状态的
        SupplierListReqVO reqVO = new SupplierListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<SupplierDO> list = supplierService.getSimpleSuppliers(reqVO);
        return success(SupplierConvert.INSTANCE.convertList03(list));
    }

}
